/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2015-3-4         yellowlgx
 */

package com.skyworth.ui.api;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.animation.TypeEvaluator;
import android.animation.ValueAnimator;
import android.animation.ValueAnimator.AnimatorUpdateListener;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.OvershootInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.skyworth.commen.ui.R;
import com.skyworth.theme.SkyThemeEngine;
import com.skyworth.theme.widget.IThemeable;
import com.skyworth.ui.blurbg.BlurBgLayout;
import com.skyworth.ui.blurbg.BlurBgLayout.PAGETYPE;
import com.skyworth.ui.blurbg.CommonBlurBG;
import com.skyworth.ui.define.SkyTextSize;
import com.skyworth.ui.menu.SkyFirstMenu;
import com.skyworth.ui.menu.SkyFirstMenu.OnItemOnkeyListener;
import com.skyworth.ui.menu.SkyMenuAdapter;
import com.skyworth.ui.menu.SkyMenuData;
import com.skyworth.ui.menu.SkyMenuItem;
import com.skyworth.ui.menu.SkySecondMenu;
import com.skyworth.ui.menu.SkySecondMenu.OnSecondItemAnimListener;
import com.skyworth.ui.menu.SkySecondMenu.OnSecondMenuItemOnkeyListener;
import com.skyworth.util.MyFocusFrame;
import com.skyworth.util.SkyScreenParams;

import java.util.List;

/**
 * @author yellowlgx
 * @ClassName SkyCommenMenu
 * @Description <p>
 * 为系统菜单公共类，其中的公共方法都是对外开放的接口，这里就不详细说明
 * </p>
 * @date 2015-3-4
 */
public class SkyCommenMenu extends FrameLayout implements IThemeable {
    private Context mContext;
    private SkyFirstMenu menu1;// 一级菜单

    private SkySecondMenu menu2; // 二级菜单

    private int menu_2_dataCount; // 二级菜单数据总数

    private OnMenuOnKeyEventListener mListener;// 事件监听

    private OnMenudismissListener menudismissListener; //菜单消失动画结束监听

    private MyFocusFrame menuFocusView; // 焦点View

    // private FocusView bgView;

    private CommonBlurBG bgLayout;

    private ImageView arrowView, titleLineView, mShadowView;

    private FrameLayout.LayoutParams mShadowP;

    private LinearLayout titleLayout;
    private TextView menuTitleView;
    private ImageView flagView;

    private int mFlagRes = -1;

    public interface OnMenuOnKeyEventListener {
        public boolean onFirstMenuItemOnKeyLeft(int itemID, View v);// 一级菜单项左键监听

        public boolean onSecondMenuItemOnKeyLeft(int itemID, View v);// 二级菜单项左键监听

        public boolean onFirstMenuItemOnKeyRight(int itemID, View v, SkyMenuData currentData);// 一级菜单项右键监听

        public boolean onSecondMenuItemOnKeyRight(int itemID, View v, SkyMenuData currentData);// 二级菜单项右键监听

        public boolean onFirstMenuItemOnClick(int itemIndex, View v, SkyMenuData currentData);// 一级菜单项点击监听

        public boolean onSecondMenuItemOnClick(int itemIndex, View v, SkyMenuData currentData);// 二级菜单项点击监听

        public boolean onFirstMenuItemOnKeyBack(int itemID, View v);// 一级菜单项返回监听

        public boolean onSecondMenuItemOnKeyBack(int itemID, View v);// 二级菜单项返回监听

        public boolean onFirstMenuItemOnKeyOther(int itemID, View v, int keyCode);

        public boolean onSecondMenuItemOnKeyOther(int itemID, View v, int keyCode);
    }

    public interface OnMenudismissListener {
        public void onMenuHiddenOver();
    }

    public void setOnMenuOnKeyEventListener(OnMenuOnKeyEventListener listener) {
        mListener = listener;
    }

    public void setOnMenuDissmissListener(OnMenudismissListener listener) {
        menudismissListener = listener;
    }

    public SkyCommenMenu(Context context) {
        super(context);
        initView(context);
    }

    public SkyCommenMenu(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public SkyCommenMenu(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initView(context);
    }

    private void initView(Context context) {
        mContext = context;
        this.setLayoutParams(
                new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

        // bgView = new FocusView(mContext);
        // bgView.setFocusable(false);
        // this.addView(bgView, new FrameLayout.LayoutParams(SkyScreenParams.getInstence(mContext)
        // .getResolutionValue(962), LayoutParams.MATCH_PARENT));
        // bgView.setVisibility(View.INVISIBLE);
        // bgView.setBackgroundResource(R.drawable.ui_sdk_menu_bg);
        // bgView.changeFocusPos(SkyScreenParams.getInstence(mContext).getResolutionValue(962),
        // SkyScreenParams.getInstence(mContext).getResolutionValue(1080));
        bgLayout = new CommonBlurBG(mContext);
        this.addView(bgLayout,
                new FrameLayout.LayoutParams(
                        SkyScreenParams.getInstence(mContext).getResolutionValue(460),
                        SkyScreenParams.getInstence(mContext).getResolutionValue(1080)));

        mShadowView = new ImageView(mContext);
        mShadowView.setBackgroundResource(R.drawable.ui_sdk_menu_leftside_shadow);
        mShadowP = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.MATCH_PARENT);
        mShadowP.leftMargin = SkyScreenParams.getInstence(mContext).getResolutionValue(460);
        this.addView(mShadowView, mShadowP);
        // bgLayout.setVisibility(View.INVISIBLE);

        menuFocusView = new MyFocusFrame(mContext,
                SkyScreenParams.getInstence(mContext).getResolutionValue(SkyViewConfig.FOCUS_VIEW_SHADER_OFFSET));
        menuFocusView.setFocusable(false);
        menuFocusView.needAnimtion(false);
        menuFocusView.setImgResourse(R.drawable.sky_focus_bg);

        FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
                SkyScreenParams.getInstence(mContext).getResolutionValue(171));
        lp.leftMargin = SkyScreenParams.getInstence(mContext).getResolutionValue(77)
                - SkyScreenParams.getInstence(mContext).getResolutionValue(47);
        lp.topMargin = SkyScreenParams.getInstence(mContext).getResolutionValue(516);
        // menuFocusView.setLayoutParams(lp);

        int x = SkyScreenParams.getInstence(mContext).getResolutionValue(52);
        int y = SkyScreenParams.getInstence(mContext).getResolutionValue(550);
        int width = SkyScreenParams.getInstence(mContext).getResolutionValue(100)
                + SkyScreenParams.getInstence(mContext).getResolutionValue(47) * 2;
        int height = SkyScreenParams.getInstence(mContext).getResolutionValue(171);

        this.addView(menuFocusView);
        menuFocusView.setVisibility(View.INVISIBLE);

        menu1 = new SkyFirstMenu(mContext);
        menu1.setTag("menu1");
        menu1.setOnItemOnkeyListener(menuListener);
        menu1.setFocusView(menuFocusView);

        menu2 = new SkySecondMenu(mContext);
        menu2.setTag("menu2");
        menu2.setSecondMenuOnItemOnkeyListener(secondMenuListener);
        menu2.setOnSecondItemAnimListener(secondItemAnimListener);
        menu2.setFocusView(menuFocusView);

        LinearLayout mainLayout = new LinearLayout(mContext);
        mainLayout.setOrientation(LinearLayout.HORIZONTAL);
        FrameLayout.LayoutParams mainP = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT);
        mainP.leftMargin = SkyScreenParams.getInstence(mContext).getResolutionValue(57);
        mainP.topMargin = SkyScreenParams.getInstence(mContext).getResolutionValue(314);
        this.addView(mainLayout, mainP);
        mainLayout.setGravity(Gravity.CENTER_VERTICAL);
        mainLayout.addView(menu1);

        LinearLayout.LayoutParams midleLayoutP = new LinearLayout.LayoutParams(
                SkyScreenParams.getInstence(mContext).getResolutionValue(40),
                LayoutParams.MATCH_PARENT);
        FrameLayout midleLayout = new FrameLayout(mContext);
        arrowView = new ImageView(mContext);
        arrowView.setFocusable(false);
        arrowView.setVisibility(View.INVISIBLE);
        arrowView.setBackground(
                SkyThemeEngine.getInstance().getDrawable(R.drawable.ui_sdk_arrow_left, true));
        FrameLayout.LayoutParams midleP = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT);
        // midleP.leftMargin = SkyScreenParams.getInstence(mContext).getResolutionValue(10);
        midleP.gravity = Gravity.CENTER_VERTICAL | Gravity.LEFT;
        // midleP.topMargin = SkyScreenParams.getInstence(mContext).getResolutionValue(603);
        arrowView.setLayoutParams(midleP);
        midleLayout.addView(arrowView);
        arrowView.setFocusable(false);
        mainLayout.addView(midleLayout, midleLayoutP);

        mainLayout.addView(menu2);
        menu2.setVisibility(View.INVISIBLE);

        titleLineView = new ImageView(mContext);
        titleLineView.setFocusable(false);
        titleLineView.setBackground(
                SkyThemeEngine.getInstance().getDrawable(R.drawable.ui_sdk_menu_title_line, true));
        FrameLayout.LayoutParams title_line_p = new FrameLayout.LayoutParams(
                SkyScreenParams.getInstence(mContext).getResolutionValue(440),
                LayoutParams.WRAP_CONTENT);
        title_line_p.topMargin = SkyScreenParams.getInstence(mContext).getResolutionValue(120);
        titleLineView.setVisibility(View.INVISIBLE);
        this.addView(titleLineView, title_line_p);

        titleLayout = new LinearLayout(mContext);
        titleLayout.setGravity(Gravity.CENTER_VERTICAL);
        titleLayout.setOrientation(LinearLayout.HORIZONTAL);
        titleLayout.setVisibility(View.INVISIBLE);

        menuTitleView = new TextView(mContext);
        menuTitleView.setFocusable(false);
        menuTitleView.setText(R.string.menu_title);
        // menuTitleView.getPaint().setFakeBoldText(true);
        menuTitleView.setTextSize(
                SkyScreenParams.getInstence(mContext).getTextDpiValue(SkyTextSize.t_8));
        menuTitleView.setTextColor(getResources().getColor(R.color.c_2));
        //        SkyScreenParams.getInstence(mContext).settextAlpha(menuTitleView, 255, "707070");

        titleLayout.addView(menuTitleView);

        flagView = new ImageView(mContext);
        // LinearLayout.LayoutParams flag_p = new
        // LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
        // LayoutParams.WRAP_CONTENT);
        // flag_p.leftMargin = SkyScreenParams.getInstence(mContext).getTextDpiValue(80);
        // titleLayout.addView(flagView, flag_p);
        FrameLayout.LayoutParams flag_p = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT);
        flag_p.leftMargin = SkyScreenParams.getInstence(mContext).getResolutionValue(81);
        flag_p.topMargin = SkyScreenParams.getInstence(mContext).getResolutionValue(150);
        this.addView(flagView, flag_p);

        FrameLayout.LayoutParams title_p = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT);
        title_p.leftMargin = SkyScreenParams.getInstence(mContext).getResolutionValue(78);
        title_p.topMargin = SkyScreenParams.getInstence(mContext).getResolutionValue(36);
        this.addView(titleLayout, title_p);
    }

    public void updataMenuTitle(String title) {
        if (menuTitleView != null && !TextUtils.isEmpty(title))
            menuTitleView.setText(title);
    }

    // 菜单名显示动画
    @SuppressLint("NewApi")
    private void showTitleAnimtion() {
        titleLineView.clearAnimation();
        TranslateAnimation lineAnim = new TranslateAnimation(
                -SkyScreenParams.getInstence(mContext).getResolutionValue(450), 0, 0, 0);
        lineAnim.setDuration(200);
        lineAnim.setInterpolator(new AccelerateInterpolator());
        titleLineView.setVisibility(View.VISIBLE);
        titleLineView.startAnimation(lineAnim);

        titleLayout.clearAnimation();
        TranslateAnimation titleAnim = new TranslateAnimation(0, 0,
                -SkyScreenParams.getInstence(mContext).getResolutionValue(120), 0);
        titleAnim.setDuration(300);
        titleAnim.setInterpolator(new OvershootInterpolator(2.0f));
        titleLayout.setVisibility(View.VISIBLE);
        titleLayout.startAnimation(titleAnim);

        // bgView.clearAnimation();
        // AlphaAnimation alphaAnimation = new AlphaAnimation(0.0f, 1.0f);
        // alphaAnimation.setDuration(200);
        // alphaAnimation.setInterpolator(new AccelerateInterpolator()); //
        // bgView.startAnimation(alphaAnimation);

        bgLayout.setLayoutParams(new FrameLayout.LayoutParams(
                SkyScreenParams.getInstence(mContext).getResolutionValue(460),
                SkyScreenParams.getInstence(mContext).getResolutionValue(1080)));
        mShadowP.leftMargin = SkyScreenParams.getInstence(mContext).getResolutionValue(460);
        mShadowView.setLayoutParams(mShadowP);
        bgLayout.setVisibility(View.VISIBLE);
        // bgLayout.animate().alpha(1.0f).setDuration(200)
        // .setInterpolator(new AccelerateInterpolator());
    }

    // 菜单消失动画
    private void dismissTitleAnimtion() {
        titleLineView.clearAnimation();
        TranslateAnimation lineAnim = new TranslateAnimation(0,
                -SkyScreenParams.getInstence(mContext).getResolutionValue(450), 0, 0);
        lineAnim.setDuration(100);
        lineAnim.setFillAfter(true);
        lineAnim.setInterpolator(new AccelerateDecelerateInterpolator());
        titleLineView.setVisibility(View.VISIBLE);
        titleLineView.startAnimation(lineAnim);

        titleLayout.clearAnimation();
        TranslateAnimation titleAnim = new TranslateAnimation(0, 0, 0,
                -SkyScreenParams.getInstence(mContext).getResolutionValue(120));
        titleAnim.setDuration(200);
        titleAnim.setFillAfter(true);
        titleAnim.setInterpolator(new AccelerateDecelerateInterpolator());
        titleLayout.setVisibility(View.VISIBLE);
        titleLayout.startAnimation(titleAnim);

        // bgLayout.setVisibility(View.VISIBLE);
        // bgLayout.animate().alpha(0.0f).setDuration(200)
        // .setInterpolator(new AccelerateInterpolator());

        FrameLayout.LayoutParams tar_p = new FrameLayout.LayoutParams(
                SkyScreenParams.getInstence(mContext).getResolutionValue(460),
                SkyScreenParams.getInstence(mContext).getResolutionValue(1080));
        changeBgViewParams(bgLayout, (LayoutParams) bgLayout.getLayoutParams(), tar_p, false);

        FrameLayout.LayoutParams lineTar_p = new FrameLayout.LayoutParams(
                SkyScreenParams.getInstence(mContext).getResolutionValue(440),
                LayoutParams.WRAP_CONTENT);
        lineTar_p.topMargin = SkyScreenParams.getInstence(mContext).getResolutionValue(120);
        changeBgViewParams(titleLineView, (LayoutParams) titleLineView.getLayoutParams(), lineTar_p,
                false);
    }

    public void setFlagViewRes(int resid) {
        if (resid <= 0) {
            flagView.setBackground(null);
            return;
        }
        mFlagRes = resid;
        flagView.setBackgroundResource(resid);
//        flagView.setBackground(SkyThemeEngine.getInstance().getDrawable(resid, true));
    }

    public void setShowingSecondMenu(boolean isShowingSecondMenu) {
        menu1.setShowingSecondMenu(isShowingSecondMenu);
    }

    public boolean getMenuDismissState() {
        return menu1 != null ? menu1.endDismiss : true;
    }

    /**
     * 显示一级菜单并设置一级菜单数据，默认第一个选项获得焦点
     *
     * @param adapter 默认数据
     * @date 2015-3-6
     */
    public void showMenu(final List<SkyMenuData> datas) {
        if (datas != null && datas.size() > 0) {
            menu1.removeAllViews();
            menu2.removeAllViews();
            if (this.getVisibility() != View.VISIBLE)
                this.setVisibility(View.VISIBLE);
            arrowView.setVisibility(View.INVISIBLE);
            menu2.isDismisAnimStart = false;
            menu2.show_disAnimStart = false;
            this.post(new Runnable() {
                @Override
                public void run() {
                    menu1.showFitstMenu(new SkyMenuAdapter(datas));
                    showTitleAnimtion();
                }
            });
        }
    }

    /**
     * 刷新一级菜具体项的值
     *
     * @param adapter 默认数据
     * @date 2015-3-26
     */
    public void refreshFirstMenuItem(SkyMenuData data, int index) {
        if (data != null) {
            if (menu1 != null) {
                menu1.refreshItemData(data, index);
            }
        }
    }

    public void setFirstMenuFocus(int index) {
        if (menu1 != null) {
            menu1.setMenuItemFocus(index);
        }
    }

    public void setSecondMenuFocus(int index) {
        if (menu2 != null) {
            menu2.setMenuItemFocus(index);
        }
    }

    /**
     * 菜单消失接口
     *
     * @date 2015-3-6
     */
    public void dismissMenu(int id) {
        menu1.dismissAnimtion(SkyCommenMenu.this, id);
        // menu2.setVisibility(View.INVISIBLE);
        dismissTitleAnimtion();
    }

    /**
     * 菜单消失接口,这个消失是由二级菜单触发的，调用此接口
     *
     * @date 2015-3-13
     */
    public void dismissMenuBySecond(int SecondMenuindex) {
        dismissTitleAnimtion();
        AlphaAnimation alphaAnim = new AlphaAnimation(1f, 0f);
        alphaAnim.setDuration(100);
        alphaAnim.setFillAfter(true);
        arrowView.startAnimation(alphaAnim);
        alphaAnim.setAnimationListener(new AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                arrowView.setVisibility(View.INVISIBLE);
                arrowView.clearAnimation();
            }
        });
        menu1.setMenuItemFocus(menu1.getLastFocusID());
        menu2.show_dismissAnimation(menu1, false, SecondMenuindex, true);
    }

    /**
     * 二级菜单显示方法
     *
     * @param datas   数据
     * @param focusId 当前默认选中项
     * @date 2015-3-6
     */
    @SuppressLint("NewApi")
    public void showSecondMenu(List<SkyMenuData> datas, int focusId) {
        if (datas != null && datas.size() > 0) {
            menu_2_dataCount = datas.size();
            moveFocusView(true, focusId, menu_2_dataCount);
            arrowView.setVisibility(View.VISIBLE);
            arrowView.animate().alpha(1.0f).setDuration(200).start();
            // AlphaAnimation alphaAnim = new AlphaAnimation(0.0f, 1.0f);
            // alphaAnim.setDuration(200);
            // alphaAnim.setFillAfter(true);
            // arrowView.startAnimation(alphaAnim);
            // alphaAnim.setAnimationListener(new AnimationListener()
            // {
            // @Override
            // public void onAnimationStart(Animation animation)
            // {
            // }
            //
            // @Override
            // public void onAnimationRepeat(Animation animation)
            // {
            // }
            //
            // @Override
            // public void onAnimationEnd(Animation animation)
            // {
            // arrowView.clearAnimation();
            // }
            // });
            setShowingSecondMenu(true);
            menu2.setMenuAdapter(menu1, new SkyMenuAdapter(datas), focusId);
            menu2.setPreViewWidth(menu1.getWidth());
            menu1.setItemStateShowSecond(true);
            // bgView.changeFocusPos(0, 0,
            // SkyScreenParams.getInstence(mContext).getResolutionValue(1200), SkyScreenParams
            // .getInstence(mContext).getResolutionValue(1080));
            FrameLayout.LayoutParams tar_p = new FrameLayout.LayoutParams(
                    SkyScreenParams.getInstence(mContext).getResolutionValue(800),
                    SkyScreenParams.getInstence(mContext).getResolutionValue(1080));
            changeBgViewParams(bgLayout, (LayoutParams) bgLayout.getLayoutParams(), tar_p, true);

            FrameLayout.LayoutParams lineTar_p = new FrameLayout.LayoutParams(
                    SkyScreenParams.getInstence(mContext).getResolutionValue(760),
                    LayoutParams.WRAP_CONTENT);
            lineTar_p.topMargin = SkyScreenParams.getInstence(mContext).getResolutionValue(120);
            changeBgViewParams(titleLineView, (LayoutParams) titleLineView.getLayoutParams(),
                    lineTar_p, true);
        }
    }

    private void changeBgViewParams(final View view, FrameLayout.LayoutParams frame_p,
                                    final FrameLayout.LayoutParams tar_p, boolean show) {
        ObjectAnimator animatorParams = ObjectAnimator.ofObject(
                new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT),
                "layoutParams", new TypeEvaluator<FrameLayout.LayoutParams>() {
                    @Override
                    public LayoutParams evaluate(float fraction, LayoutParams startValue,
                                                 LayoutParams endValue) {
                        FrameLayout.LayoutParams tarFp = new FrameLayout.LayoutParams(
                                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
                        if (startValue == null || endValue == null) {
                            return tarFp;
                        }
                        tarFp.leftMargin = (int) (startValue.leftMargin
                                + fraction * (endValue.leftMargin - startValue.leftMargin));
                        tarFp.topMargin = (int) (startValue.topMargin
                                + fraction * (endValue.topMargin - startValue.topMargin));
                        tarFp.width = (int) (startValue.width
                                + fraction * (endValue.width - startValue.width));
                        tarFp.height = (int) (startValue.height
                                + fraction * (endValue.height - startValue.height));
                        return tarFp;
                    }
                }, frame_p, tar_p);
        animatorParams.addUpdateListener(new AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                FrameLayout.LayoutParams tarFp = (LayoutParams) animation.getAnimatedValue();
                view.setLayoutParams(tarFp);
                if (view instanceof BlurBgLayout) {
                    mShadowP.leftMargin = tarFp.width;
                    mShadowView.setLayoutParams(mShadowP);
                }
            }
        });
        animatorParams.addListener(new AnimatorListenerAdapter() {

            @Override
            public void onAnimationCancel(Animator animation) {
                super.onAnimationCancel(animation);
                view.setLayoutParams(tar_p);
                if (view instanceof BlurBgLayout) {
                    if (mShadowP.leftMargin != tar_p.width) {
                        mShadowP.leftMargin = tar_p.width;
                        mShadowView.setLayoutParams(mShadowP);
                    }
                }
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                view.setLayoutParams(tar_p);
                if (view instanceof BlurBgLayout) {
                    if (mShadowP.leftMargin != tar_p.width) {
                        mShadowP.leftMargin = tar_p.width;
                        mShadowView.setLayoutParams(mShadowP);
                    }
                }
                view.animate().cancel();
            }

        });

        animatorParams.setInterpolator(new AccelerateInterpolator());
        if (show)
            animatorParams.setDuration(100);
        else
            animatorParams.setDuration(100);
        animatorParams.start();
    }

    /**
     * 刷新二级菜具体项的值
     *
     * @param adapter 默认数据
     * @date 2015-3-26
     */
    public void refreshSecondMenuItem(SkyMenuData data, int index) {
        if (data != null) {
            if (menu2 != null) {
                menu2.refreshItemData(data, index);
            }
        }
    }

    private void moveFocusView(boolean isShowSecond, int index, int count) {
        // TranslateAnimation transAnim = null;
        // menuFocusView.clearAnimation();
        // int moveX = menu1.getWidth() +
        // SkyScreenParams.getInstence(mContext).getResolutionValue(50);
        //
        // int start = 0, end = 0;
        // if ((index - 3) >= 0)
        // {
        // start = index - 3;
        // } else
        // {
        // index = 3;
        // start = 0;
        // }
        // if ((index + 3) <= count)
        // {
        // end = index + 4;
        // } else
        // {
        // end = count;
        // }
        // int size = end - start;
        // if (index >= size)
        // index = size;
        // int duration = 100 + (((index) * 200) / size);
        // if (isShowSecond)
        // {
        // transAnim = new TranslateAnimation(0, moveX, 0, 0);
        // // transAnim.setDuration(500);
        // transAnim.setDuration(duration);
        // transAnim.setFillAfter(true);
        // menuFocusView.startAnimation(transAnim);
        // } else
        // {
        // transAnim = new TranslateAnimation(moveX, 0, 0, 0);
        // // transAnim.setDuration(300);
        // transAnim.setDuration(duration);
        // transAnim.setFillAfter(true);
        // menuFocusView.startAnimation(transAnim);
        // }
    }

    // 一级菜单监听
    OnItemOnkeyListener menuListener = new OnItemOnkeyListener() {
        @Override
        public boolean onItemOnKeyLeft(int itemID, View v) {
            if (menu2.isDismisAnimStart || menu2.show_disAnimStart) {
                return true;
            }
            if (mListener != null)
                mListener.onFirstMenuItemOnKeyLeft(itemID, v);
            return true;
        }

        @Override
        public boolean onItemOnKeyRight(int itemID, View v, SkyMenuData currentData) {
            Log.v("lgx", "menu2.isDismisAnimStart ==  " + menu2.isDismisAnimStart);
            Log.v("lgx", "menu2.show_disAnimStart ==  " + menu2.show_disAnimStart);
            if (menu2.isDismisAnimStart || menu2.show_disAnimStart) {
                return true;
            }
            if (currentData.isHasSecondMenu()) {
                setShowingSecondMenu(true);
            }
            ((SkyMenuItem) v).setSelectState(true);
            menu1.setLastFocusItemID(itemID);
            if (mListener != null)
                mListener.onFirstMenuItemOnKeyRight(itemID, v, currentData);
            return true;
        }

        @Override
        public boolean onItemOnClick(int itemIndex, View v, SkyMenuData currentData) {
            if (menu2.isDismisAnimStart || menu2.show_disAnimStart) {
                return true;
            }
            if (currentData.isHasSecondMenu()) {
                setShowingSecondMenu(true);
            }
            ((SkyMenuItem) v).setSelectState(true);
            menu1.setLastFocusItemID(itemIndex);
            if (mListener != null)
                mListener.onFirstMenuItemOnClick(itemIndex, v, currentData);
            return true;
        }

        @Override
        public boolean onItemOnKeyBack(int itemID, View v) {
            if (menu2.isDismisAnimStart || menu2.show_disAnimStart) {
                return true;
            }
            Log.v("lgx", "on First menu on key Back !!!!!!!!!!!!!");
            menu1.resetOnKeyListener();
            menu2.dismissBySecondLeft_back = false;
            if (mListener != null) {
                return mListener.onFirstMenuItemOnKeyBack(itemID, v);
            } else
                return false;
        }

        @Override
        public boolean onItemOnKeyOther(int itemID, View v, int keyCode) {
            if (mListener != null)
                return mListener.onFirstMenuItemOnKeyOther(itemID, v, keyCode);
            else
                return false;
        }

        @Override
        public void onFirstMenuHiddenOver() {
            if (menudismissListener != null)
                menudismissListener.onMenuHiddenOver();
        }
    };

    // 二级菜单监听
    OnSecondMenuItemOnkeyListener secondMenuListener = new OnSecondMenuItemOnkeyListener() {

        @Override
        public boolean onSecondMenuItemOnKeyLeft(int itemID, View v) {
            if (menu2.isDismisAnimStart || menu2.show_disAnimStart) {
                return true;
            }
            // bgView.changeFocusPos(0, 0,
            // SkyScreenParams.getInstence(mContext).getResolutionValue(962), SkyScreenParams
            // .getInstence(mContext).getResolutionValue(1080));
            FrameLayout.LayoutParams tar_p = new FrameLayout.LayoutParams(
                    SkyScreenParams.getInstence(mContext).getResolutionValue(460),
                    SkyScreenParams.getInstence(mContext).getResolutionValue(1080));
            changeBgViewParams(bgLayout, (LayoutParams) bgLayout.getLayoutParams(), tar_p, false);

            FrameLayout.LayoutParams lineTar_p = new FrameLayout.LayoutParams(
                    SkyScreenParams.getInstence(mContext).getResolutionValue(440),
                    LayoutParams.WRAP_CONTENT);
            lineTar_p.topMargin = SkyScreenParams.getInstence(mContext).getResolutionValue(120);
            changeBgViewParams(titleLineView, (LayoutParams) titleLineView.getLayoutParams(),
                    lineTar_p, false);

            if (menu1 != null)
                menu1.setMenuItemFocus(menu1.getLastFocusID());
            menu2.show_dismissAnimation(menu1, false, itemID, false);
            // AlphaAnimation alphaAnim = new AlphaAnimation(1f, 0f);
            // alphaAnim.setDuration(200);
            // alphaAnim.setFillAfter(true);
            // arrowView.startAnimation(alphaAnim);
            arrowView.animate().alpha(0.0f).setDuration(200).start();
            menu1.setItemStateShowSecond(false);
            menu1.setShowingSecondMenu(false);
            moveFocusView(false, itemID, menu_2_dataCount);
            if (mListener != null)
                mListener.onSecondMenuItemOnKeyLeft(itemID, v);
            menu2.isDismisAnimStart = true;
            menu2.dismissBySecondLeft_back = true;
            return true;
        }

        @Override
        public boolean onSecondMenuItemOnKeyRight(int itemID, View v, SkyMenuData currentData) {
            if (mListener != null)
                return mListener.onSecondMenuItemOnKeyRight(itemID, v, currentData);
            else
                return false;
        }

        @Override
        public boolean onSecondMenuItemOnClick(int itemIndex, View v, SkyMenuData currentData) {
            menu2.dismissBySecondLeft_back = false;
            if (mListener != null)
                mListener.onSecondMenuItemOnClick(itemIndex, v, currentData);
            return true;
        }

        @Override
        public boolean onSecondMenuItemOnKeyBack(int itemID, View v) {
            if (menu2.isDismisAnimStart || menu2.show_disAnimStart) {
                return true;
            }
            // bgView.changeFocusPos(0, 0,
            // SkyScreenParams.getInstence(mContext).getResolutionValue(962), SkyScreenParams
            // .getInstence(mContext).getResolutionValue(1080));
            FrameLayout.LayoutParams tar_p = new FrameLayout.LayoutParams(
                    SkyScreenParams.getInstence(mContext).getResolutionValue(460),
                    SkyScreenParams.getInstence(mContext).getResolutionValue(1080));
            changeBgViewParams(bgLayout, (LayoutParams) bgLayout.getLayoutParams(), tar_p, false);

            FrameLayout.LayoutParams lineTar_p = new FrameLayout.LayoutParams(
                    SkyScreenParams.getInstence(mContext).getResolutionValue(440),
                    LayoutParams.WRAP_CONTENT);
            lineTar_p.topMargin = SkyScreenParams.getInstence(mContext).getResolutionValue(120);
            changeBgViewParams(titleLineView, (LayoutParams) titleLineView.getLayoutParams(),
                    lineTar_p, false);

            Log.v("lgx", "on Second menu on key Back !!!!!!!!!!!!!");
            if (menu1 != null)
                menu1.setMenuItemFocus(menu1.getLastFocusID());
            menu2.show_dismissAnimation(menu1, false, itemID, false);
            // AlphaAnimation alphaAnim = new AlphaAnimation(1f, 0f);
            // alphaAnim.setDuration(200);
            // alphaAnim.setFillAfter(true);
            // arrowView.startAnimation(alphaAnim);
            arrowView.animate().alpha(0.0f).setDuration(200).start();
            menu1.setItemStateShowSecond(false);
            menu1.setShowingSecondMenu(false);
            moveFocusView(false, itemID, menu_2_dataCount);
            if (mListener != null)
                mListener.onSecondMenuItemOnKeyBack(itemID, v);
            menu2.isDismisAnimStart = true;
            menu2.dismissBySecondLeft_back = true;
            return true;
        }

        @Override
        public boolean onSecondMenuOnKeyOther(int itemID, View v, int keyCode) {
            if (mListener != null)
                return mListener.onSecondMenuItemOnKeyOther(itemID, v, keyCode);
            else
                return false;
        }
    };

    OnSecondItemAnimListener secondItemAnimListener = new OnSecondItemAnimListener() {
        @Override
        public void onSecondItemAnimEnd(boolean isShow) {
            if (!isShow)
                menu1.dismissAnimtion(SkyCommenMenu.this, menu1.getLastFocusID());
        }
    };

    @Override
    public void refreshOnThemeChanged() {
        if (arrowView != null)
            arrowView.setBackground(
                    SkyThemeEngine.getInstance().getDrawable(R.drawable.ui_sdk_arrow_left, true));
        if (titleLineView != null)
            titleLineView.setBackground(SkyThemeEngine.getInstance()
                    .getDrawable(R.drawable.ui_sdk_menu_title_line, true));
        if (menuTitleView != null)
            menuTitleView.setTextColor(getResources().getColor(R.color.c_2));
//        if (flagView != null && mFlagRes > 0)
//            flagView.setBackground(SkyThemeEngine.getInstance().getDrawable(mFlagRes, true));

        if (menu1 != null)
            menu1.refreshOnThemeChanged();

        if (menu2 != null)
            menu2.refreshOnThemeChanged();
    }

    public void destroy() {
        if (bgLayout != null) {
            bgLayout.destroy();
        }
    }

}
